package cn.sundat.service;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import cn.sundat.dao.CompanyDao;
import cn.sundat.model.Company;
import cn.sundat.model.PageBean;

@Transactional
public class CompanyServiceImpl implements CompanyService{
	@Resource(name="companyDao")
	private CompanyDao companyDao;
	
	public void save(Company company) {
		companyDao.save(company);
	}
	
	public Company getById(Long id) {
		return companyDao.getById(id);
	}

	/**
	 * 检查账号的状态
	 */
	public int checkLogin(String username,String password) {
		//先只判断账号
		DetachedCriteria criteria = DetachedCriteria.forClass(Company.class);
		criteria.add(Restrictions.eq("comp_name", username));
		int i = companyDao.checkLogin(criteria);
		//如果为0，再判断一次
		if(i==0) {
			DetachedCriteria criteria2 = DetachedCriteria.forClass(Company.class);
			criteria2.add(Restrictions.eq("comp_code", username));
			i = companyDao.checkLogin(criteria2);
		}
		
		//账号审核过,则去判断是否密码正确
		if(i==2) {
			List<Company> list = login(username, password);
			if(list.size()<=0) {
				i=3;
			}
		}
		return i;
	}
	
	public List<Company> login(String comp_name, String comp_pwd) {
		//先用名字去登录
		List<Company> list = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(Company.class);
		criteria.add(Restrictions.eq("comp_name", comp_name));
		criteria.add(Restrictions.eq("comp_pwd", comp_pwd));
		list = companyDao.login(criteria);
		
		//当名字登录失败，用代码去登录
		if(list.size()<=0) {
			DetachedCriteria criteria2 = DetachedCriteria.forClass(Company.class);
			criteria2.add(Restrictions.eq("comp_code", comp_name));
			criteria2.add(Restrictions.eq("comp_pwd", comp_pwd));
			list = companyDao.login(criteria2);
		}
		
		return list;
	}

	public void update(Company company) {
		companyDao.update(company);
	}
	
	/**
	 * 根据名字获公司信息
	 */
	public Company getByName(String comp_name) {
		return companyDao.findByName(comp_name);
	}
	
	public List<Company> listCompany(){
		return companyDao.listCompany();
	}

	@Override
	public void delById(Long id) {
		// TODO Auto-generated method stub
		companyDao.delete(companyDao.getById(id));
	}

	@Override
	public List<Company> findCompanyByKey(String key,String value) {
		// TODO Auto-generated method stub
		return companyDao.findCompanyByKey(key,value);
	}

	@Override
	public List<Company> auditList() {
		// TODO Auto-generated method stub
		return companyDao.auditList();
	}

	@Override
	public List<Company> findCompanyById(Long id) {
		// TODO Auto-generated method stub
		return companyDao.findCompanyById(id);
	}
	
	@Override
	public PageBean<Company> findByLevel(int currPage, Integer pageSize, int level) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Company.class);
		criteria.add(Restrictions.eq("comp_level", level));
		return companyDao.findByLevel(criteria, currPage,pageSize);
	}
	
	
	@Override
	public Company findById(Long comp_id) {
		return companyDao.getById(comp_id);
	}

	@Override
	public void regApprove(Long id) {
		// TODO Auto-generated method stub
		companyDao.regApprove(id);
	}
	public PageBean<Company> findPbByName(Integer currPage, Integer pageSize, DetachedCriteria criteria) {
		return companyDao.findPbByName(currPage,pageSize,criteria);
	}

	@Override
	public void ApproveUpdateLevel(Long id, Integer nextLevel) {
		// TODO Auto-generated method stub
		companyDao.ApproveUpdateLevel(id, nextLevel);
	}

	@Override
	public List<Company> updateLevelList() {
		// TODO Auto-generated method stub
		return companyDao.updateLevelList();
	}
	
}


